﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Transactions;

namespace Catering
{
    public class View_Operaction_UpFile
    {
        #region 声明变量
        private static string _UpFileFolder = string.Empty;
        private static string _UpPotoFolder = string.Empty;
        public static string UpFileFolder
        {
           
            get
            {
                return Catering_DBOpertion.PubConstant.GetConfigValue("UpFilePath");
            }
        }
        public static string UpPotoFolder
        {
            get
            {
                return Catering_DBOpertion.PubConstant.GetConfigValue("UpFilePoto");
            }
        }
        #endregion

        #region 上传方法
        /// <summary>
        /// 上传电子表格
        /// </summary>
        /// <param name="fu_party">控件名称</param>
        /// <returns></returns>
        public static string uploadexcel(FileUpload fu_party,out string message)
        {
            string msg=string.Empty;
            string time = string.Empty;
            string name = string.Empty;
            string path = null;
            string fileextenison = string.Empty;
            if (fu_party.HasFile)
            {
                name = fu_party.FileName.ToString();
                path = fu_party.PostedFile.FileName.ToString();
                fileextenison = System.IO.Path.GetExtension(path);
                if (fileextenison != ".xls")
                {
                     msg = "文件类型不正确！";
                }
                else
                {
                    time = System.DateTime.Now.ToString();
                    time = time.Replace("/", "");
                    time = time.Replace(":", "");
                    time = time.Replace(" ", "");
                   
                    int i = name.LastIndexOf(".");
                    name = name.Remove(i);
                    name = name + time + ".xls";

                    path = System.Web.HttpContext.Current.Server.MapPath("~/" + Catering_DBOpertion.PubConstant.GetConfigValue("UpFilePath") + "/") + name;
                    fu_party.SaveAs(path);
                  
                }

            }
            else
            {
               
                msg = "请选择上传文件！";
            }
            message = msg;
            return name;

        }
        /// <summary>
        /// 上传指定文件类型的图片或者文件
        /// </summary>
        /// <param name="fu_party">上传控件</param>
        /// <param name="strFileType">文件类型</param>        
        /// <returns></returns>
        public static string uploadeFile(FileUpload fu_party, string strFileType,out string message)
        {
            string msg=string.Empty;
            string time = string.Empty;
            string name = string.Empty;
            string path = null;
            string fileextenison = string.Empty;
            if (fu_party.HasFile)
            {
                name = fu_party.FileName.ToString();
                path = fu_party.PostedFile.FileName.ToString();
                fileextenison = System.IO.Path.GetExtension(path);
                if (!fileextenison.Equals(strFileType))
                {
                    msg = "文件类型不正确！";
                }
                else
                {
                    time = System.DateTime.Now.ToString();
                    time = time.Replace("/", "");
                    time = time.Replace(":", "");
                    time = time.Replace(" ", "");

                    int i = name.LastIndexOf(".");
                    name = name.Remove(i);
                    name = name + time + ".xls";
                    string bootname = string.Empty;
                    if (strFileType.Equals(".xls") || strFileType.Equals(".xlsx"))
                    {
                        bootname = Catering_DBOpertion.PubConstant.GetConfigValue("UpFilePath");
                    }
                    else
                    {
                        bootname = Catering_DBOpertion.PubConstant.GetConfigValue("UpFilePoto");
                    }
                    path = System.Web.HttpContext.Current.Server.MapPath("~/" + bootname + "/") + name;
                    fu_party.SaveAs(path);

                }

            }
            else
            {

                msg = "请选择上传文件！";
            }
            message = msg;
            return name;

        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fu_party">控件名称</param>
        /// <returns></returns>
        public static string uploadPoto(FileUpload fu_party,out string message)
        {
            string msg=string.Empty;
            string time=string.Empty;
            string name=string.Empty;
            string path = null;
            string fileextenison =string.Empty;
            if (fu_party.HasFile)
            {
                name = fu_party.FileName.ToString();
                path = fu_party.PostedFile.FileName.ToString();
                fileextenison = System.IO.Path.GetExtension(path);
                if (fileextenison != ".jpg")
                {
                    msg = "文件类型不正确！";
                }
                else
                {
                    time = System.DateTime.Now.ToString();
                    time = time.Replace("/", "");
                    time = time.Replace(":", "");
                    time = time.Replace(" ", "");

                    int i = name.LastIndexOf(".");
                    name = name.Remove(i);
                    name = name + time + ".jpg";

                    path = System.Web.HttpContext.Current.Server.MapPath("~/" + Catering_DBOpertion.PubConstant.GetConfigValue("UpFilePoto") + "/") + name;
                    fu_party.SaveAs(path);

                }

            }
            else
            {

                msg = "请选择上传文件！";
            }
            message = msg;
            return name;

        }
        #endregion
    }
}